<?php
	class Checklist{
		var $url;
		var $short_checklist_rating;
		var $usability_checklist_rating;
		var $accessbility_checklist_rating;

		public function Checklist( $id = NULL, $user_id = NULL, $url = NULL){
			require_once("functions.php");
			$con = getdb();
			if( !empty($id) ) {
				require_once("functions.php");
				$con = getdb();
				$sql = pg_query($con, "SELECT * FROM checklists WHERE id = ".$id."");
				if(pg_num_rows($sql) < 1){
					pg_close($con);
					return false;
				}
				else
				{
					$this->id = $id;
					$checklist = pg_fetch_row($sql);
					$this->user_id = $checklist[1];
					$sql_user = pg_query($con, "SELECT * FROM usuarios WHERE usuarios.id = ".$this->user_id."");
					$user = pg_fetch_row($sql_user);
					$this->url = $checklist[2];
					$this->short_checklist_rating = $checklist[3];
					$this->usability_checklist_rating = $checklist[4];
					$this->accessbility_checklist_rating = $checklist[5];
					pg_close($con);
					return true;
				}
			}
			if ( ( !empty($user_id) ) && ( !empty($url) ) ){
				$result = pg_query($con, "INSERT INTO checklists(user_id, url, created_at) VALUES (".$user_id.",'".$url."', NOW())");
				if(!$result){
					pg_close($con);
					return false;
				}
				else {
					$query = pg_query($con, "SELECT * FROM checklists WHERE user_id = ".$user_id." AND url = '".$url."'");
					$checklist = pg_fetch_row($query);
					$this->user_id = $user_id;
					$this->url = $url;
					$this->id = $checklist[0];
					$this->short_checklist_rating = $checklist[3];
					$this->usability_checklist_rating = $checklist[4];
					$this->accessbility_checklist_rating = $checklist[5];
					$this->created_at = $checklist[6];
					pg_close($con);
					return true;
				}
			}
			else{
				pg_close($con);
				return false;
			}

		}

		public function getChecklist($id = NULL){

		}

		public function getUrl(){
			return $this->url;
		}
		public function getShortChecklistRating(){
			return $this->short_checklist_rating;
		}
		public function getUsabilityChecklistRating(){
			return $this->usability_checklist_rating;
		}
		public function getAccessbilityChecklistRating(){
			return $this->accessbility_checklist_rating;
		}
		public function getUser(){
			$con = getdb();
			$sql = pg_query($con, "SELECT * FROM usuarios WHERE usuarios.id =".$this->user_id."");
			pg_close($con);
			return $sql;
		}

		public function size(){
			$con = getdb();
			$sql = pg_query($con, "SELECT COUNT(*) FROM checklists");
			return $sql;
			pg_close($con);
		}

		public function setShortChecklistRating($short_checklist_rating){
			$this->short_checklist_rating = $short_checklist_rating;
			$con = getdb();
			$sql = pg_query($con, "UPDATE checklists SET short_checklist_rating = ".$short_checklist_rating." WHERE id = ".$this->id." ");
			pg_close($con);
		}

		public function setUsabilityChecklistRating($usability_checklist_rating){
			$this->usability_checklist_rating = $usability_checklist_rating;
			$con = getdb();
			$sql = pg_query($con, "UPDATE checklists SET usability_checklist_rating = ".$usability_checklist_rating." WHERE id = ".$this->id." ");
			pg_close($con);
		}

		
		public function __destruct(){
		}


	}
?>